package com.dlsp.hotel.controller.admin;

import com.dlsp.hotel.bean.Account;
import com.dlsp.hotel.service.AccountService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author:fanxh
 * @date: 2021/12/31/14:12
 */
@Controller
@RequestMapping("/admin/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping("/accountUi")
    public String AccountUi(Model model){
        List<Account> accountList = accountService.queryAll();
        model.addAttribute("accountList",accountList);
        return  "pages/account_manager";
    }

    @RequestMapping("/findByName")
    public String accountName(String accountName ,Model model){
        List<Account> accountList = accountService.findByNameLike(accountName);
        model.addAttribute("accountList",accountList);
        return  "pages/account_manager";
    }
    @RequestMapping("/addaccount")
    public String addaccount(Account account,Model model){
        accountService.add(account);
        return  "redirect:/admin/account/accountUi";
    }

    @RequestMapping("/editaccount")
    public String editaccount(Account account,Model model){
        accountService.edit(account,account.getId());
        List<Account> accountList = accountService.queryAll();
        model.addAttribute("accountList",accountList);
        return  "redirect:/admin/account/accountUi";
    }

    @RequestMapping("/deleteaccount")
    public String deleteaccount(Integer id){
        int res = accountService.delete(id);
        return  "redirect:/admin/account/accountUi";
    }


    /**
     * 分页查询客户信息
     * @param
     * @param
     */
//    @RequestMapping(value="/getAccountList")
//    public String getAccountList(Integer startpage,Integer pagesize,Model model){
//        PageHelper.startPage(startpage,pagesize);
//        List<Account> accountList = accountService.queryAll();
//        PageInfo<Account> accountPageInfo =
//                new PageInfo<>(accountList);
//        model.addAttribute("accountList",accountList);
//        model.addAttribute("accountPageInfo",accountPageInfo);
//        return "pages/account_manager";
//    }

//    @RequestMapping("/accountUi")
//    public String accountUi(Model model,
//                         @RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
//                         @RequestParam(defaultValue = "15",value = "pageSize") Integer pageSize) {
//        //设置默认当前页
//        if(pageNum==null || pageNum<=0){
//            pageNum = 1;
//        }
//        //设置默认每页显示的数据数
//        if(pageSize == null){
//            pageSize = 15;
//        }
//
//        //引入分页插件，pageNum是第几页，pageSize是每页显示多少条
//        PageHelper.startPage(pageNum,pageSize);
//        try {
//            List<Account> accountList = accountService.queryAll();
//            //使用PageInfo包装查询后的结果，15是连续显示的条数，结果list类型是Page<E>
//            PageInfo<Account> pageInfo = new PageInfo<Account>(accountList,pageSize);
//            //使用model带回前端
//            model.addAttribute("pageInfo",pageInfo);
//            model.addAttribute("accountList", accountList);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            PageHelper.clearPage();
//        }
//        return "pages/account_manager";
//    }

}
